package com.fosun.spr.core.proxy.aop;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.FieldError;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 

import com.fosun.spr.core.utils.ExtBeanUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * 
 * @Title: aop切面基类.java
 * @Description:
 * @author zhangcheng
 * @date 2017年3月14日 下午2:21:43
 * @version V1.0
 * 
 */
public abstract class BaseAspect {

	protected static final Logger logger = LoggerFactory
			.getLogger(BaseAspect.class);

	private static Gson gson = new GsonBuilder()
			.setDateFormat("yyyy-MM-dd HH:mm:ss").disableHtmlEscaping()
			.serializeNulls().setPrettyPrinting().create();

	protected static String gsonTostring(Object object) {
		try {
			return gson.toJson(object);
		} catch (Exception e) {
			logger.error("aop切面解析json字符串异常:{}", e);
		}
		return "{}";
	}

	protected static HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
	}

	protected static String getUserAgent() {
		return getRequest().getHeader("user-agent");
	}

	protected static HttpServletResponse getResponse() {
		return ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getResponse();
	}

	/**
	 * 
	 * @Title: getRemoteId
	 * @Description: 获取ip真实地址
	 * @return
	 * @return String
	 */
	protected static String getRemoteId() {

		HttpServletRequest request = getRequest();
		String unknown = "unknown";
		if (request == null) {
			logger.error("获取request为空!");
			return unknown;
		}
		String zero = "0:0:0:0:0:0:0:1";
		String sp = ",";
		String ip = request.getHeader("Cdn-Src-Ip");
		logger.info("getRemoteAddrIp : Cdn-Src-Ip = " + ip);
		if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
			ip = request.getHeader("X-Forwarded-For");
			logger.info("getRemoteAddrIp : X-Forwarded-For = " + ip);
		}
		if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
			logger.info("getRemoteAddrIp : Proxy-Client-IP = " + ip);
		}
		if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
			logger.info("getRemoteAddrIp : WL-Proxy-Client-IP = " + ip);
		}
		if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
			logger.info("getRemoteAddrIp : HTTP_CLIENT_IP = " + ip);
		}
		if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
			logger.info("getRemoteAddrIp : HTTP_X_FORWARDED_FOR = " + ip);
		}
		if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
			ip = request.getHeader("X-Real-IP");
			logger.info("getRemoteAddrIp : X-Real-IP = " + ip);
		}
		if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_REAL_IP");
			logger.info("getRemoteAddrIp : HTTP_X_REAL_IP = " + ip);
		}
		if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
			logger.info("getRemoteAddrIp : remoteAddr = " + ip);
		}
		if (ip.equals(zero)) {
			ip = "127.0.0.1";
		}
		// 如果是多级代理，那么取第一个IP为客户IP
		if (ip != null && ip.indexOf(sp) != -1) {
			String[] strs = ip.split(sp);
			if (strs != null && strs.length > 0) {
				ip = strs[0];
			}
		}
		logger.info("getRemoteAddrIp : ip = " + ip);
		return ip;
	}

	protected static String getValiaErrorMsg(List<FieldError> fieldErrorList) {

		StringBuffer stringBuffer = new StringBuffer();

		if (!ExtBeanUtils.isEmptyContainer(fieldErrorList)) {
			for (FieldError fieldError : fieldErrorList) {
				stringBuffer.append("field: " + fieldError.getField()
						+ " >> errorMsg:" + fieldError.getDefaultMessage());
			}
		}
		return stringBuffer.toString();

	}

}
